<?php
namespace Tbf\Bundle\DatabaseBundle\Tests\ListQuery;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Yaml\Yaml;
use Tbf\Bundle\DatabaseBundle\Tests\TbfDatabaseBundleTestCase;

class FunctionalTest extends TbfDatabaseBundleTestCase
{
    public function test1()
    {
        $qb = new QueryBuilder($this->getMockEm());
        $qb->from('\abc\abc','a')
            ->select('a');
        $new_qb = $this->newListQueryHandler()
            ->fromOrmQueryBuilder($qb)
            ->fromListQueryArray(array())
            ->getQueryBuilder();
        $this->assertTrue($qb!==$new_qb);
        $this->assertEquals('SELECT a FROM \abc\abc a',$new_qb->getDQL());
    }
    public function testAndLike()
    {
        $qb = new QueryBuilder($this->getMockEm());
        $qb->from('\abc\abc','a')
            ->select('a');
        $new_qb = $this->newListQueryHandler()
            ->fromOrmQueryBuilder($qb)
            ->fromListQueryArray(Yaml::parse(<<<YML
page: 6
pagesize: 10
where:
  type: AndLikeCombineConditionalExpress
  value:
    - {field:a,value:王}
    - {field:b,value:赵}
order:
  - {field:a,direction:desc}
  - {field:b,direction:asc}
YML
))
            ->getQueryBuilder();
        $this->assertTrue($qb!==$new_qb);
        $this->assertEquals('SELECT a FROM \abc\abc a',$qb->getDQL());
        $this->assertEquals('SELECT a FROM \abc\abc a WHERE a.a LIKE :holder_1 AND a.b LIKE :holder_2',
            $new_qb->getDQL());
        $this->assertEquals('%王%',$new_qb->getParameter('holder_1')->getValue());
        $this->assertEquals('%赵%',$new_qb->getParameter('holder_2')->getValue());
    }
    public function testInSimple()
    {
        $qb = new QueryBuilder($this->getMockEm());
        $qb->from('\abc\abc','a')
            ->select('a');
        $new_qb = $this->newListQueryHandler()
            ->fromOrmQueryBuilder($qb)
            ->fromConditionalExpressArray(Yaml::parse(<<<YML
type: InSimpleConditionalExpress
value:
  field: id
  value: [1,3,5,6]
YML
            ))
            ->getQueryBuilder();
        $this->assertTrue($qb!==$new_qb);
        $this->assertEquals('SELECT a FROM \abc\abc a',$qb->getDQL());
        $this->assertEquals('SELECT a FROM \abc\abc a WHERE a.id IN(:holder_1,:holder_2,:holder_3,:holder_4)',
            $new_qb->getDQL());
        $this->assertEquals(1,$new_qb->getParameter('holder_1')->getValue());
        $this->assertEquals(3,$new_qb->getParameter('holder_2')->getValue());
        $this->assertEquals(5,$new_qb->getParameter('holder_3')->getValue());
        $this->assertEquals(6,$new_qb->getParameter('holder_4')->getValue());
    }
    public function testAllLike()
    {
        $this->markTestIncomplete();
        $qb = new QueryBuilder($this->getMockEm());
        $qb->from('\abc\abc','a')
            ->select('a');
        $new_qb = $this->newListQueryHandler()
            ->fromOrmQueryBuilder($qb)
            ->fromConditionalExpressArray(Yaml::parse(<<<YML
type: AllLikeConditionalExpress
value: 赵
YML
            ))
            ->getQueryBuilder();
        $this->assertTrue($qb!==$new_qb);
        $this->assertEquals('SELECT a FROM \abc\abc a',$qb->getDQL());
        $this->assertEquals('SELECT a FROM \abc\abc a WHERE a.id like :holder_1 or a.title like :holder_2',
            $new_qb->getDQL());
        $this->assertEquals('%赵%',$new_qb->getParameter('holder_1')->getValue());
        $this->assertEquals('%赵%',$new_qb->getParameter('holder_2')->getValue());
    }
    public function getMockEm()
    {
        $ref = new \ReflectionClass('\Doctrine\ORM\EntityManager');

        return $ref->newInstanceWithoutConstructor();
    }
    /**
     * @return \Tbf\Bundle\DatabaseBundle\ListQuery\ListQueryHandler
     */
    public function newListQueryHandler()
    {
        return $this->get('tbf_database.list_query')->create();
    }
}
